package com.demo;


import apache.rocketmq.v2.Resource;
import jakarta.annotation.PostConstruct;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer_delay {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //发送延时消息
    public void sendMessage(String topic, String message) {
        //发送任意时间的延时消息
        Message msg = new Message(topic,  message.getBytes());
        msg.setDeliverTimeMs(System.currentTimeMillis() + 1000 * 10);
        try {
            rocketMQTemplate.getProducer().send(msg);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("发送延时消息成功 时间："+ System.currentTimeMillis());
    }

//    @PostConstruct
    public void init() throws MQClientException {
        sendMessage("my-topic-delay", "Hello, delay!");
    }
}
